package com.cslg.ac.demo.dao.jedis;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
@PropertySource(value = {"classpath:redis.properties"},encoding="utf-8")
public class JedisDAO implements InitializingBean{

	@Value("${redisUrl}")
	private String redisUrl;
	
	private JedisPool jedisPool;

	@Override
	public void afterPropertiesSet() {
		jedisPool = new JedisPool(redisUrl);
	}
	
	private Jedis getConnection()
	{
		Jedis jedis = null;
		jedis = jedisPool.getResource();
		return jedis;
	}


	public void incr(String key){
		Jedis jedis = getConnection();
		jedis.incr(key);
		jedis.close();
	}

	public Long bitcout(String key){
		Jedis jedis = getConnection();
		Long day = jedis.bitcount(key);
		jedis.close();
		return day;
	}

	public boolean getbit(String key,long offset){
		Jedis jedis = getConnection();
		boolean flag = jedis.getbit(key,offset);
		jedis.close();
		return flag;
	}


	public void setbit(String key,long offset,String value){

		Jedis jedis = getConnection();
		jedis.setbit(key,offset,value);
		jedis.close();
	}

	public String get(String key)
	{
		Jedis jedis = getConnection();
		String value = jedis.get(key);
		jedis.close();
		return value;
	}
	
	public void del(String key)
	{
		Jedis jedis = getConnection();
		jedis.del(key);
		jedis.close();
	}

	public void setex(String key, int time, String value)
	{
		Jedis jedis = getConnection();
		jedis.setex(key, time, value);
		jedis.close();
	}
	
	public void set(String key, String value)
	{
		Jedis jedis = getConnection();
		jedis.set(key, value);
		jedis.close();
	}

	public void flushAll(){
		Jedis jedis = getConnection();
		jedis.flushAll();
		jedis.close();
	}
}
